﻿using System;
using System.ComponentModel;
using System.Xml;
using System.Globalization;

namespace MMK.Paket_Tracker.Runtime
{
    /// <summary>
    /// Class for SerialData
    /// </summary>
    public class SerialData : INotifyPropertyChanged
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="SerialData"/> class.
        /// </summary>
        /// <param name="node">The node.</param>
        public SerialData(XmlNode node)
        {
            startPos = Int32.Parse(node.Attributes["StartPosValue"].Value);
            stopPos = Int32.Parse(node.Attributes["StopPosValue"].Value);
            startUnit = Int32.Parse(node.Attributes["StartPosUnit"].Value);
            stopUnit = Int32.Parse(node.Attributes["StopPosUnit"].Value);
            divider = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator.ToCharArray()[0];
        }
        /// <summary>
        /// Gets or sets the raw data.
        /// </summary>
        /// <value>The raw data.</value>
        public String RawData 
        {
            get
            { 
                return rawData; 
            }
            set
            {
                Double balValue;
                rawData = value;
                //OnPropertyChanged("RawData");
                if (Double.TryParse(rawData.Substring(startPos, stopPos - startPos).Replace('.', divider), out balValue)) ;
                {
                    BalValue = balValue;
                    Unit = rawData.Substring(startUnit, stopUnit - startUnit);
                }
            }
        }
        /// <summary>
        /// Gets or sets the unit.
        /// </summary>
        /// <value>The unit.</value>
        public String Unit 
        { 
            get
            {
                return unit;
            }
            set
            {
                unit = value;
                //OnPropertyChanged("Value");
            }
        }
        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public Double BalValue 
        {
            get
            { 
                return balValue; 
            }
            set
            { 
                balValue = value;
                //OnPropertyChanged("BalValue");
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="name">The name.</param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        private String rawData;
        private String unit;
        private Double balValue;
        private Int32 startPos, stopPos, startUnit, stopUnit;
        private Char divider;
    }
}
